using System;
using System.Collections.Generic;

namespace PackageResources
{
    public class SerializableHeader
    {
        private List<byte> _headerList; //头数组

        public SerializableHeader()
        {
            _headerList = new List<byte>();
        }

        /// <summary>
        /// 添加一条信息
        /// </summary>
        /// <param name="filePath">文件位置</param>
        /// <param name="index">位置索引</param>
        /// <param name="length">长度</param>
        public void Add(string filePath, long index, long length)
        {
            //注意顺序, 因为之前的是固定的, 所以可以不使用分隔符
            byte[] indexBytes = BitConverter.GetBytes(index);
            byte[] lengthBytes = BitConverter.GetBytes(length);
            byte[] filePathBytes = System.Text.Encoding.UTF8.GetBytes(filePath);
            byte[] filePathLengthBytes = BitConverter.GetBytes(filePathBytes.LongLength); //filePath长度
            _headerList.AddRange(indexBytes);
            _headerList.AddRange(lengthBytes);
            _headerList.AddRange(filePathLengthBytes);
            _headerList.AddRange(filePathBytes);
        }

        public byte[] ToBytes()
        {
            return _headerList.ToArray();
        }
    }
}